package cc.zy.base.businesses.service.impl;

import cc.zy.base.businesses.entity.*;
import cc.zy.base.businesses.mapper.*;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.businesses.service.ICollegeService;
import cc.zy.base.common.utils.SortUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.*;

/**
 * 院校
 * Service
 *
 * @author Jiangjinlin
 * @date 2021-01-18 10:51:13
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class CollegeServiceImpl extends ServiceImpl<CollegeMapper, College> implements ICollegeService {

    @Resource
    private final CollegeMapper collegeMapper;
    private final LevelMapper levelMapper;
    private final MajorMapper majorMapper;
    private final BatchMapper batchMapper;
    private final DicMapper dicMapper;

    @Override
    public IPage<College> findColleges(QueryRequest request, College college) {
        Page<College> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countCollegeDetail(college));
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return baseMapper.findCollegeDetailPage(page, college);
    }

    @Override
    public College findById(Integer id) {
        return baseMapper.findById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteColleges(String[] collegeIds) {
        List<String> list = Arrays.asList(collegeIds);
        // 删除用户
        this.removeByIds(list);
    }

    @Override
    public List<College> findCollageListNotPage(College college) {
        List<College> collageListNotPage = baseMapper.findCollageListNotPage(college);
        return collageListNotPage;
    }

    @Override
    public College findCollegeDetailList(Integer id) {
        College param = new College();
        param.setId(id);
        List<College> college = baseMapper.findCollegeDetail(param);
        return CollectionUtils.isNotEmpty(college) ? college.get(0) : null;
    }

    @Override
    public List<College> findColleges(College college) {
        LambdaQueryWrapper<College> queryWrapper = new LambdaQueryWrapper<>();

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCollege(College college) {
        college.setCreateDate(new Date());
        college.setUpdateDate(new Date());
        this.save(college);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCollege(College college) {
        this.saveOrUpdate(college);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCollege(College college) {
        LambdaQueryWrapper<College> wrapper = new LambdaQueryWrapper<>();

        this.remove(wrapper);
    }

    /**
     * 方法描述：方法描述：查询所有院校[id、名称]
     *
     * @return java.util.List<cc.zy.base.businesses.entity.College> 院校集合
     * @author LiPeng
     * @date 2021/3/9
     */
    @Override
    public List<College> findCollegeForSelect() {

        return collegeMapper.selectCollegeForSelect();
    }

    /**
     * 方法描述：根据院校id查询层次[id、名称]，正序
     *
     * @param collegeId 院校id【必传项】
     * @return java.util.List<cc.zy.base.businesses.entity.Level> 层次集合
     * @author LiPeng
     * @date 2021/3/9
     */
    @Override
    public List<Level> findLevelForSelect(Integer collegeId) {

        return levelMapper.selectLevelForSelect(collegeId);
    }

    /**
     * 方法描述：根据院校id、层次id查询专业[id、名称]
     *
     * @param collegeId 院校id【必传项】
     * @param levelId   层次id【必传项】
     * @return java.util.List<cc.zy.base.businesses.entity.Major> 专业集合
     * @author LiPeng
     * @date 2021/3/9
     */
    @Override
    public List<Major> findMajorForSelect(Integer collegeId, Integer levelId) {

        return majorMapper.selectMajorForSelect(collegeId, levelId);
    }

    /**
     * 方法描述：获取所有有效批次[id、名称]，倒序
     *
     * @return java.util.List<cc.zy.base.businesses.entity.Batch> 批次集合
     * @author LiPeng
     * @date 2021/3/6
     */
    @Override
    public List<Batch> selectBatchForWx() {

        return batchMapper.selectBatchForWx();
    }

    /**
     * 方法描述：根据学生信息获取当前学生有效批次[id、名称]，倒序
     *
     * @return java.util.List<cc.zy.base.businesses.entity.Batch> 批次集合
     * @author JIANGJINLIN
     * @date 2021/3/6
     */
    @Override
    public List<Batch> selectBatchByStuForWx(String studentName, String identity) {

        return batchMapper.selectBatchByStuForWx(studentName, identity);
    }

    /**
     * 方法描述：根据学生信息获取当前学生有效批次[id、名称]，倒序不包含当前登录信息
     *
     * @return java.util.List<cc.zy.base.businesses.entity.Batch> 批次集合
     * @author JIANGJINLIN
     * @date 2021/3/6
     */
    @Override
    public List<Batch> selectBatchByStuButThisForWx(String studentName, String identity, String batchName, String studyTypeName) {

        return batchMapper.selectBatchByStuButThisForWx(studentName, identity, batchName, studyTypeName);
    }

    /**
     * 方法描述：获取所有有效批次[id、名称]，倒序
     *
     * @return java.util.List<cc.zy.base.businesses.entity.Batch> 批次集合
     * @author LiPeng
     * @date 2021/3/6
     */
    @Override
    public List<Batch> findBatchForSelect() {

        return batchMapper.selectBatchForSelect();
    }

    @Override
    public Boolean isRepetitive(String name, String simpleName) {
        List<College> repetitive = collegeMapper.isRepetitive(name, simpleName);
        Boolean flag = true;
        if (repetitive != null && repetitive.size() > 0) {
            flag = false;
        }
        return flag;
    }

    /**
     * 方法描述：查询所有学习形式
     *
     * @return java.util.List<cc.zy.base.businesses.entity.Dic> 学习形式集合
     * @author LiPeng
     * @date 2021/3/9
     */
    @Override
    public List<Dic> findStudyTypeForSelect() {

        return dicMapper.findDicByType("study_type");
    }

    /**
     * 方法描述：查询校区
     *
     * @return java.util.List<cc.zy.base.businesses.entity.Dic> 校区集合
     * @author LiPeng
     * @date 2021/3/18
     */
    @Override
    public List<Dic> findCampusForSelect() {

        return dicMapper.findDicByType("campus");
    }
}